//
//  KNewsGeneralController.m
//  Keping
//
//  Created by a on 2017/9/15.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KNewsGeneralController.h"
#import "HJWebViewController.h"
#import "KNewsGeneralDetailController.h"

#import "SDCycleScrollView.h"
#import "KUserData.h"

#import "KAdViewCell.h"
#import "KGeneralViewCell.h"

#import "KbbsTopicsModel.h"

#import "KBbsTopicsRequest.h"
@interface KNewsGeneralController ()<SDCycleScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

/**广告视图*/
@property(nonatomic,weak)SDCycleScrollView*adView;
/**tableView*/
@property(nonatomic,weak)UITableView*tableView;
/** 公告数据模型 */
@property(nonatomic,strong)NSMutableArray<bbsTopicViews *> *bbsTopicsModel;
/** 轮播图模型 */
@property(nonatomic,strong)NSMutableArray<bbsTopicViews *> *adModel;
/**页码*/
@property(nonatomic,assign)NSInteger pageOn;

@end

@implementation KNewsGeneralController


- (NSMutableArray<bbsTopicViews *> *)bbsTopicsModel {
    if (!_bbsTopicsModel) {
        _bbsTopicsModel = [[NSMutableArray alloc] init];
    }
    return _bbsTopicsModel;
}

- (NSMutableArray<bbsTopicViews *> *)adModel {
    if (!_adModel) {
        _adModel = [[NSMutableArray alloc] init];
    }
    return _adModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化页面为1
    self.pageOn=1;
    
    [self setUI];
    
    [self getData];
    //获取轮播图
    [self getAd];
}
-(void)setUI
{
    UITableView*tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.tableFooterView=[UIView new];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    
    SDCycleScrollView*adView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];//[UIImage imageNamed:KImagePlaceholder]
    adView.pageControlDotSize = CGSizeMake(10, 10);
    adView.pageDotColor = kWhiteColor;
    adView.currentPageDotColor = kYellowColor;
    adView.autoScrollTimeInterval = 2.5;
    adView.delegate = self;
    tableView.tableHeaderView=adView;
    self.adView=adView;
}
-(void)getData
{
    [super getData];
    [self showLoading];
    
    KBbsTopicsRequest* bbsTopicsRequest = [KBbsTopicsRequest new];
    WEAKSELF;
    KPartyView* user = [KPartyView unarchive];
    //公告=100000007000014
    bbsTopicsRequest.params = @{@"cateId":@"100000007000014",@"communityId":user.communityId?user.communityId:@"",@"pageOn":@(self.pageOn)};
    [bbsTopicsRequest startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        //type1
        NSDictionary *dict = request.responseObject;
        NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
        if (status == 200) {
            NSArray *arr = [dict objectForKey:@"bbsTopicViews"];
            for (int i = 0; i < arr.count; i++) {
                NSDictionary *tmp = [arr objectAtIndex:i];
                bbsTopicViews *bbs = [bbsTopicViews modelWithDictionary:tmp];
                [weakSelf.bbsTopicsModel addObject:bbs];
            }
            [weakSelf.tableView reloadData];
        } else {
            [weakSelf showErrorText:[dict objectForKey:@"msg"] errCode:[[dict objectForKey:@"statusCode"] integerValue]];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        [weakSelf showError:request.error];
    }];
}
-(void)getAd{
    KBbsTopicsRequest* bbsTopicsRequest = [KBbsTopicsRequest new];
    WEAKSELF;
    KPartyView* user = [KPartyView unarchive];
    //100000007000005=轮播图
    bbsTopicsRequest.params = @{@"cateId":@"100000007000005",@"communityId":user.communityId?user.communityId:@""};
    [bbsTopicsRequest startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        //type1
        
        NSDictionary *dict = request.responseObject;
        NSInteger status = [[dict objectForKey:@"statusCode"] integerValue];
        if (status == 200) {
            NSMutableArray *array = [NSMutableArray new];
            NSMutableArray *imgArray = [NSMutableArray new];
            NSArray *arr = [dict objectForKey:@"bbsTopicViews"];
            for (int i = 0; i < arr.count; i++) {
                NSDictionary *tmpDict = [arr objectAtIndex:i];
                bbsTopicViews *bbs = [bbsTopicViews modelWithDictionary:tmpDict];
                [array addObject:bbs];
                [imgArray addObject:[KGetImageURL stringByAppendingString:bbs.img]];
            }
            weakSelf.adView.imageURLStringsGroup = imgArray;
            weakSelf.adModel = [array mutableCopy];
        } else {
            [self hideHUD];
            [weakSelf showErrorText:[dict objectForKey:@"msg"] errCode:[[dict objectForKey:@"statusCode"] integerValue]];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [self hideHUD];
        [self showError:request.error];
    }];
}
-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.adView.frame=CGRectMake(0, 0, kScreenWidth,K_FactorH(200));
    self.tableView.frame=self.view.bounds;
    self.tableView.tableHeaderView=self.adView;
}
#pragma mark -- SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    //查看公告详情
    bbsTopicViews*bbsTopicViewsModel=self.adModel[index];
    if ([bbsTopicViewsModel.isAd isEqualToString:@"Y"]) {
        KWebViewController *web = [[KWebViewController alloc] init];
        web.requestUrl = bbsTopicViewsModel.summary;
        web.title = bbsTopicViewsModel.subject;
        [self.navigationController pushViewController:web animated:YES];
    } else {
        KNewsGeneralDetailController*vc=[KNewsGeneralDetailController new];
        vc.bbsTopicViewsID=bbsTopicViewsModel.id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark --UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bbsTopicsModel.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    bbsTopicViews*bbsTopicViewsModel=self.bbsTopicsModel[indexPath.row];
    if ([bbsTopicViewsModel.isAd isEqualToString:@"Y"]) {
        //广告
        KAdViewCell*cell=[KAdViewCell cellWithTableView:tableView];
        cell.bbsTopicViews=self.bbsTopicsModel[indexPath.row];
       return cell;
    }else{
        KGeneralViewCell*cell=[KGeneralViewCell cellWithTableView:tableView];
        cell.bbsTopicViews=self.bbsTopicsModel[indexPath.row];
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    bbsTopicViews*bbsTopicViewsModel=self.bbsTopicsModel[indexPath.row];
     if ([bbsTopicViewsModel.isAd isEqualToString:@"Y"]) {
         return K_FactorH(450/2);
     }
    return K_FactorH(115);
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    bbsTopicViews*bbsTopicViewsModel=self.bbsTopicsModel[indexPath.row];
        //查看公告详情
    KNewsGeneralDetailController*vc=[KNewsGeneralDetailController new];
    vc.bbsTopicViewsID=bbsTopicViewsModel.id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
